<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/19 0019
 * Time: 下午 1:25
 */

namespace app\common;


use app\common\model\User;

class Auth
{
    static $user;
    static function getUserId(){
        $user = self::getUser();
        if($user){
            return $user->id;
        }
        return -1;
    }
    static function getUser(){
        if(empty(self::$user)){
            $user_id = session('user_id');
            self::$user = User::get($user_id);
        }
        return self::$user;
    }
    static function setUser($user){
        if(is_numeric($user)){
            session('user_id',$user);
        }else if(isset($user->id)){
            session('user_id',$user->id);
        }
    }
    static function logout(){
        session('user_id',-1);
    }
    static function pwd($key,$salt = "ctrl"){
        return md5($key.$salt);
    }
}